﻿using LiveCharts;
using LiveCharts.Wpf;
using MyFirstWPF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyFirstWPF.ViewModel;
using System.Globalization;
using SQLite;

namespace MyFirstWPF.View
{
    /// <summary>
    /// Page3.xaml 的交互逻辑
    /// </summary>
    public partial class Page3 : Page
    {
         public static string dbPath = "../../studyPlan.db";

        Page3Model page3Model  = new Page3Model();
        public Page3()
        {
            InitializeComponent();
            this.DataContext = page3Model;
            initialDayHarvestData();
            initialWeekHarvestData();
            initialTotalFocusData();
            initialTotalHarvestData();
            initialDayFocusData();
            initialWeekFocusData();
        }

        //private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
        //{
        //    var chart = (LiveCharts.Wpf.PieChart)chartpoint.ChartView;

        //    //clear selected slice.
        //    foreach (PieSeries series in chart.Series)
        //        series.PushOut = 0;

        //    var selectedSeries = (PieSeries)chartpoint.SeriesView;
        //    selectedSeries.PushOut = 8;
        //}
        private void initialDayHarvestData()
        {
            using(var db = new StudyPlanDb(dbPath))
            {
                int _dayHarvest = 0;
                //CultureInfo provider = CultureInfo.InvariantCulture;
                //String format = "yyyy-MM-dd HH:mm:ss";
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //每日收成统计
                for (int i = 0; i < taskList.Count; i++)
                {
                    if (DateTime.Parse(taskList[i].EndTime).Date == DateTime.Now.Date&&taskList[i].isFinished == 1) _dayHarvest++;
                }
                DayHarvest.Text = _dayHarvest.ToString(); 
            }
        }
        private void initialWeekHarvestData()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                int _weekHarvest = 0;
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //每周收成统计
                for (int i = 0; i < taskList.Count; i++)
                {
                    //TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Now;
                    //if (timeSpan.Days > -7 && timeSpan.Days < 1) _weekHarvest++;
                    //DateTime dt1 = DateTime.Now;
                    //DateTime dt2 = dt1.AddYears(3);          //时间加3年
                    //int ct1 = dt1.CompareTo(dt2);                 //dt1早于dt2,返回-1；
                    //int ct2 = dt2.CompareTo(dt1);                  //dt2晚于dt1,返回1；
                    //int ct3 = dt2.CompareTo(dt1);             //dt2与dt1相等，返回0；
                    //DateTime dt3 = dt1.AddYears(4);             //时间加4年
                    //bool b = Equals(dt2);                          //dt1与dt2不相等，返回false
                    int min = DateTime.Parse(taskList[i].EndTime).CompareTo(DateTime.Now.AddDays(-7));
                    int max = DateTime.Parse(taskList[i].EndTime).CompareTo(DateTime.Now);
                    if (min == 1 && max == -1 && taskList[i].isFinished == 1) _weekHarvest++;
                }
                WeekHarest.Text = _weekHarvest.ToString();
            }
        }
        private void initialTotalHarvestData()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                int _totalHarvest = 0;
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //总收成
                for (int i = 0; i < taskList.Count; i++)
                {
                   if(taskList[i].isFinished == 1) _totalHarvest++;
                }
                TotalHarvest.Text = _totalHarvest.ToString();
            }
        }
        private void initialDayFocusData()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                double _dayFocus = 0;
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //今日专注时间
                for (int i = 0; i < taskList.Count; i++)
                {
                    TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Parse(taskList[i].StartTime);
                    TimeSpan timeSpan1 = DateTime.Parse(taskList[i].EndTime).TimeOfDay - DateTime.Parse(taskList[i].StartTime).TimeOfDay;
                    int min = DateTime.Parse(taskList[i].StartTime).Date.CompareTo(DateTime.Now.Date);
                    int max = DateTime.Parse(taskList[i].EndTime).Date.CompareTo(DateTime.Now.Date);
                    if(taskList[i].isFinished == 1)
                    {
                        if (DateTime.Parse(taskList[i].EndTime).Date == DateTime.Now.Date &&
                        timeSpan.TotalHours <= 24)
                        {
                        _dayFocus += timeSpan.TotalHours;
                        }
                        else if (timeSpan.TotalHours > 24&&min == -1 && (max == 1 || max == 0))
                        {
                        _dayFocus += timeSpan1.TotalHours;
                        } 
                    }
                }
                DayFocus.Text = _dayFocus.ToString("0.00");
            }
        }
        private void initialWeekFocusData()
        {
            double _weekFocus = 0;
            using (var db = new StudyPlanDb(dbPath))
            {
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //每周专注时间 
                for (int i = 0; i < taskList.Count; i++)
                {
                    int min = DateTime.Parse(taskList[i].EndTime).Date.CompareTo(DateTime.Now.AddDays(-8).Date);
                    int max = DateTime.Parse(taskList[i].EndTime).Date.CompareTo(DateTime.Now.Date);
                    TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Parse(taskList[i].StartTime);
                    TimeSpan timeSpan1 = DateTime.Parse(taskList[i].EndTime).TimeOfDay - DateTime.Parse(taskList[i].StartTime).TimeOfDay;
                    if(taskList[i].isFinished == 1)
                    {
                        //小于24小时的任务的EndTime.Date必须落在本周内
                        if (timeSpan.TotalHours <= 24 && min == 1 && max == -1) _weekFocus += timeSpan.TotalHours;
                        //用户只能设置时间左小右大的跨两天以上的任务
                        else if ((min == -1 || min == 0) && (max == -1))
                        {
                            TimeSpan span = DateTime.Parse(taskList[i].EndTime).Date - DateTime.Now.AddDays(-8).Date;
                            int times = span.Days;
                            _weekFocus += timeSpan1.TotalHours * times;
                        }
                        else if (min == 1 && (max == 1 || max == 0))
                        {
                            TimeSpan span = DateTime.Now.Date - DateTime.Parse(taskList[i].StartTime).Date;
                            int times = span.Days;
                            _weekFocus += timeSpan1.TotalHours * times;
                        }
                        else if ((min == -1 || min == 0) && (max == 1 || max == 0))
                        {
                            _weekFocus += timeSpan1.TotalHours * 7;
                        }
                        else if (timeSpan.TotalHours >= 24 && min == 1 && max == -1)
                        {
                            TimeSpan span = DateTime.Parse(taskList[i].EndTime).Date - DateTime.Parse(taskList[i].StartTime).Date;
                            int times = span.Days;
                            _weekFocus += timeSpan1.TotalHours * times;
                        }
                    } 
                }
                WeekFocus.Text = _weekFocus.ToString("0.00");
            }
        }
        private void initialTotalFocusData()
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                double _totalFocus = 0;
                List<TaskInfo> taskList = Enumerable.Where(db.Tasks, x =>
                {
                    return x.userId == LoginView.myId;
                }).ToList();
                //总专注时间
                for (int i = 0; i < taskList.Count; i++)
                {
                    TimeSpan timeSpan = DateTime.Parse(taskList[i].EndTime) - DateTime.Parse(taskList[i].StartTime);
                    TimeSpan timeSpan1 = DateTime.Parse(taskList[i].EndTime).TimeOfDay - DateTime.Parse(taskList[i].StartTime).TimeOfDay;
                    if(taskList[i].isFinished == 1)
                    {
                        if (timeSpan.TotalHours <= 24) _totalFocus += timeSpan.TotalHours;
                        if (timeSpan.TotalHours > 24) _totalFocus += timeSpan1.TotalHours;
                    }
                }
                TotalFocus.Text = _totalFocus.ToString("0.00");
                //
            }
        }
    }
}
